package cn.sxw.android.base.utils;

import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import cn.sxw.android.base.ui.UserAgreementActivity;

/**
 * Created by zhanghai on 2019/12/22.
 * function：
 */
public class UserAgreementUtils {

    private final static String TEXT_AGREEMENT_INTRO = "登录表示已阅读并同意生学《用户协议》和《隐私政策》";
    private final static String TEXT_AGREEMENT_INTRO_PROFILE = "生学素质教育平台《用户协议》和《隐私政策》";
    private final static String TEXT_USER_AGREE = "《用户协议》";
    private final static String TEXT_PRIVATE_AGREE = "《隐私政策》";
    private static final int FOCUS_COLOR = 0xFF12C8C8;

    public final static int TYPE_USER_AGREEMENT = 1;
    public final static int TYPE_USER_PRIVATE_AGREEMENT = 2;
    public final static String EXTRA_FLAG = "agreementType";

    private UserAgreementUtils() {

    }

    public static void addUserAgreement(@NonNull TextView textView) {
        addUserAgreement(textView, false);
    }

    public static void addUserAgreementProfile(@NonNull TextView textView) {
        addUserAgreement(textView, true);
    }

    private static void addUserAgreement(@NonNull TextView textView, boolean isProfile) {
        final SpannableStringBuilder style = new SpannableStringBuilder();

        //设置文字
        if (isProfile) {
            style.append(TEXT_AGREEMENT_INTRO_PROFILE);
        } else {
            style.append(TEXT_AGREEMENT_INTRO);
        }
        //设置部分文字点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                //点击了用户协议
                Intent intent = new Intent(textView.getContext(), UserAgreementActivity.class);
                intent.putExtra(EXTRA_FLAG, TYPE_USER_AGREEMENT);
                textView.getContext().startActivity(intent);
            }
        };
        int start0;
        if (isProfile) {
            start0 = TEXT_AGREEMENT_INTRO_PROFILE.indexOf(TEXT_USER_AGREE);
        } else {
            start0 = TEXT_AGREEMENT_INTRO.indexOf(TEXT_USER_AGREE);
        }
        int end0 = start0 + TEXT_USER_AGREE.length();
        style.setSpan(clickableSpan, start0, end0, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置部分文字颜色
        ForegroundColorSpan fcs = new ForegroundColorSpan(FOCUS_COLOR);
        style.setSpan(fcs, start0, start0 + TEXT_USER_AGREE.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置部分文字点击事件
        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                //点击了隐私协议
                Intent intent = new Intent(textView.getContext(), UserAgreementActivity.class);
                intent.putExtra(EXTRA_FLAG, TYPE_USER_PRIVATE_AGREEMENT);
                textView.getContext().startActivity(intent);
            }
        };
        int start2;
        if (isProfile) {
            start2 = TEXT_AGREEMENT_INTRO_PROFILE.indexOf(TEXT_PRIVATE_AGREE);
        } else {
            start2 = TEXT_AGREEMENT_INTRO.indexOf(TEXT_PRIVATE_AGREE);
        }
        int end2 = start2 + TEXT_PRIVATE_AGREE.length();
        style.setSpan(clickableSpan2, start2, end2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置部分文字颜色
        ForegroundColorSpan fcs2 = new ForegroundColorSpan(FOCUS_COLOR);
        style.setSpan(fcs2, start2, end2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //配置给TextView
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(style);
    }
}
